﻿#pragma once 
#include "Comm.h"
#include "Buffer.h"

namespace COMM
{
	class COMM_API BmpBuffer
	{
	public:
		BmpBuffer(const void* bmp, int size);
		BmpBuffer(const char* filepath);
		BmpBuffer(const unsigned char* raw, int width, int height, int channels = 1);
		~BmpBuffer();
		operator Buffer();

		bool isValid();
		unsigned char* get();
		int size();

		int rowBytes();
		int width();
		int height();
		int channels();
		int headerSize();
		int rawSize();
		unsigned char* rawBuffer();
		void reverseRaw();

		static int RowBytes(int width, int channels = 1); // 4-bytes aligned width.also mean step.
		static void Reverse(unsigned char* dst, const unsigned char* src, int width, int height, int channels = 1);
		static void Resize(unsigned char* raw_dst, int dst_width, int dst_height,
			const unsigned char* raw_src, int src_width, int src_height, int channels = 1);
		static void NV12ToRGB(unsigned char * rgb24_dst, const unsigned char * yuv_src, int width, int height);
		static void RGB2GRAY(unsigned char* gray8_dst, const unsigned char* rgb24_src, int width, int height);
		static void BGR2RGB(unsigned char* raw_dst, const unsigned char* raw_src, int width, int height);

	private:
		void Load(const unsigned char* raw, int width, int height, int channels, unsigned char** pbuffer, int* psize);
		void Load(const char* filepath, unsigned char** pbuffer, int* psize);

	private:
		unsigned char* m_buffer;
		int m_size;
	};

}
